data<-read.csv('GSE25066_expr_all.csv',quote='',header=T,row.names=1,check.names=F)
phe<-read.csv('GSE25066_sample.csv',quote='',header=T,row.names=1,check.names=F)
data<-as.data.frame(t(data))
data$sample<-rownames(data)
data<-merge(phe,data,by='sample')
rownames(data)<-data$sample
phe<-data[,c(2,26)]
data<-data[,-1:-26]
data<-as.data.frame(t(data))
library(genefilter)
library(GSVA)
library(Biobase)
library(stringr)
gene_set<-read.csv("mmc3.csv",quote='',header=T,check.names=F)
list<- split(as.matrix(gene_set)[,1], gene_set[,2])
gsva_matrix<- gsva(as.matrix(data), list,method='ssgsea',abs.ranking=TRUE)
gsva<- as.data.frame(scale(t(gsva_matrix)))
gsva$sample<-rownames(gsva)

write.table(data,'data.txt',quote=F,sep='\t')
library(estimate)
filterCommonGenes(input.f='data.txt', output.f="BRCA.gct", id="GeneSymbol")
estimateScore(input.ds="BRCA.gct", output.ds="BRCA_estimate_score.gct")
estimate_score <- read.table("BRCA_estimate_score.gct", skip = 2, header = TRUE)
rownames(estimate_score) <- estimate_score[,1]
estimate_score <- estimate_score[,3:ncol(estimate_score)]
es<-as.data.frame(t(estimate_score))
es$sample<-gsub('\\.','-',rownames(es))
cell<-merge(gsva,es,by='sample')
phe$sample<-rownames(phe)
phe<-merge(phe,cell,by='sample')
library(reshape2)
phe$group<-ifelse(phe$group=='low','Low','High')
c<-phe[,c(3,32:34)]
c<-melt(c,id.vars='group')
library(ggsignif)
library(ggforce)
library(ggpubr)
pdf('cell_score.pdf',width=6,height=6)
ggboxplot(c, x="variable", y="value",outlier.shape = NA,
          fill = "group",xlab = "", ylab = "TME score") +
    stat_compare_means(aes(group= group),label = "p.signif",label.y = 5000,
                       symnum.args = list(cutpoints = c(0, 0.0001, 0.001, 0.01, 0.05, 1), 
                                          symbols = c("****", "***", "**", "*", "ns"))) +
    scale_fill_manual(values = c('red','blue')) +
    theme_classic()+
    #theme(legend.position = "none")  +
    theme(axis.text.x = element_text(angle = 45, hjust = 1))
dev.off()
library(ggpubr)
pdf('TumorPurity.pdf',width=5,height=5)
ggboxplot(phe, x = "group", y = "TumorPurity",fill = 'group', palette = c('red','blue'))+stat_compare_means(comparisons=list(c('High','Low')),label = "p.signif")+labs(x='',y='TumorPurity')
dev.off()
c<-phe[,c(3:31)]
c<-melt(c,id.vars='group')
pdf('cell_gsva.pdf',width=12,height=6)
ggboxplot(c, x="variable", y="value",outlier.shape = NA,
          fill = "group",xlab = "GSE25066 Immune Cells", ylab = "GSVA score") +
    stat_compare_means(aes(group= group),label = "p.signif",label.y = 6,
                       symnum.args = list(cutpoints = c(0, 0.0001, 0.001, 0.01, 0.05, 1), 
                                          symbols = c("****", "***", "**", "*", " "))) +
    scale_fill_manual(values =c('red','blue')) +
    theme_classic()+
    #theme(legend.position = "none")  +
    theme(axis.text.x = element_text(angle = 45, hjust = 1))
dev.off()

cor<-cor(phe$rs,phe[,32:35])
library(psych)
p<-corr.test(phe$rs,phe[,32:35], adjust = "none", use = "complete")
p<-as.data.frame(p$p.adj)
pdf('ImmuneScore_piont.pdf',width=5,height=4.5)
ggplot(phe,aes(ImmuneScore,rs))+
    geom_point(aes(colour=group))+
    geom_smooth(method = "lm",color = "black")+
    theme_bw()+labs(x='ImmuneScore',y='Risk Score')+
    annotate("text",x = 3000, y =2  ,label = 'R = -0.044',color = "black")+
    annotate("text",x = 3000, y = 1.7,label = 'P = 0.556',color = "black")+scale_color_manual(values =c('red','blue'))
dev.off()
pdf('StromalScore_piont.pdf',width=5,height=4.5)
ggplot(phe,aes(StromalScore,rs))+
    geom_point(aes(colour=group))+
    geom_smooth(method = "lm",color = "black")+
    theme_bw()+labs(x='StromalScore',y='Risk Score')+
    annotate("text",x = 1300, y =2 ,label = 'R = 0.014',color = "black")+
    annotate("text",x = 1300, y = 1.7,label = 'P = 0.854',color = "black")+scale_color_manual(values =c('red','blue'))
dev.off()
pdf('ESTIMATEScore_piont.pdf',width=5,height=4.5)
ggplot(phe,aes(ESTIMATEScore,rs))+
    geom_point(aes(colour=group))+
    geom_smooth(method = "lm",color = "black")+
    theme_bw()+labs(x='ESTIMATEScore',y='Risk Score')+
    annotate("text",x = 3800, y =2  ,label = 'R = -0.014',color = "black")+
    annotate("text",x = 3800, y = 1.7,label = 'P = 0.849',color = "black")+scale_color_manual(values =c('red','blue'))
dev.off()
pdf('TumorPurity_piont.pdf',width=5,height=4.5)
ggplot(phe,aes(TumorPurity,rs))+
    geom_point(aes(colour=group))+
    geom_smooth(method = "lm",color = "black")+
    theme_bw()+labs(x='TumorPurity',y='Risk Score')+
    annotate("text",x = 0.8, y =2  ,label = 'R = 0.015',color = "black")+
    annotate("text",x = 0.8, y = 1.7,label = 'P = 0.847',color = "black")+scale_color_manual(values =c('red','blue'))
dev.off()

lm22<-read.table('LM22.txt',header=T,row.names=1,quote='',check.names=F,sep='\t')
gene<-as.data.frame(rownames(lm22))
names(gene)<-'id'
data$id<-rownames(data)
data<-merge(data,gene,by='id')
rownames(data)<-data$id
data<-data[,-1]
write.table(data,'data.txt',quote=F,sep='\t')
source('CIBERSORT.R')
results <- CIBERSORT('LM22.txt','data.txt', perm=500, QN=F)
cell1<-as.data.frame(results)
cell1<-cell1[,-23:-25]
phe<-phe[,c(1,3)]
cell1$sample<-rownames(cell1)
cell1<-merge(phe,cell1,by='sample')
cell1<-cell1[order(cell1$group),]
c1<-melt(cell1[,-1],id.vars='group')
pdf('cell_CIBERSORT.pdf',width=12,height=6)
ggboxplot(c1, x="variable", y="value",outlier.shape = NA,
          fill = "group",xlab = "GSE25066 Immune Cells", ylab = "Cell Proportion") +
    stat_compare_means(aes(group= group),label = "p.signif",label.y = 0.61,
                       symnum.args = list(cutpoints = c(0, 0.0001, 0.001, 0.01, 0.05, 1), 
                                          symbols = c("****", "***", "**", "*", " "))) +
    scale_fill_manual(values = c('red','blue')) +
    theme_classic()+
    #theme(legend.position = "none")  +
    theme(axis.text.x = element_text(angle = 45, hjust = 1))
dev.off()

timer<-read.csv('estimation_matrix.csv',quote='',header=T,row.names=1,check.names=F)
timer<-timer[1:6,]
timer<-as.data.frame(t(timer))
timer$sample<-rownames(timer)
timer<-merge(phe,timer,by='sample')
rownames(timer)<-timer$sample
timer<-timer[,-1]
c2<-melt(timer,id.vars='group')
pdf('cell_timer.pdf',width=8,height=6)
ggboxplot(c2, x="variable", y="value",outlier.shape = NA,
          fill = "group",xlab = "GSE25066 Immune Cells", ylab = "Cell Proportion") +
    stat_compare_means(aes(group= group),label = "p.signif",label.y = 0.55,
                       symnum.args = list(cutpoints = c(0, 0.0001, 0.001, 0.01, 0.05, 1), 
                                          symbols = c("****", "***", "**", "*", " "))) +
    scale_fill_manual(values = c('red','blue')) +
    theme_classic()+
    #theme(legend.position = "none")  +
    theme(axis.text.x = element_text(angle = 45, hjust = 1))
dev.off()
rm(c,c1,c2,data,estimate_score,gene,gene_set,gsva_matrix,list,lm22,CIBERSORT,CoreAlg,doPerm,results)
